//
//  DSPStatusViewModel.swift
//  SWweibo
//
//  Created by 杜守鹏 on 16/1/3.
//  Copyright © 2016年 杜守鹏. All rights reserved.
//

import UIKit

class DSPStatusViewModel: NSObject {

    var status:DSPStatus?{
        didSet{
            // 计算会员要显示的图片
            if let mbrank = status?.user?.mbrank where mbrank > 0 && mbrank < 7 {
                memberImage = UIImage(named: "common_icon_membership_level\(mbrank)")
            }else{
                // 设置会员图标
                memberImage = nil
            }
            // 设置认证的图标
            verifiedImage = calcVerifiedImage()
            //设置转发评论赞的数量字符串
            repostsCount = calcStatusCount(status?.reposts_count ?? 0 , defaultTitle: "转发")
            commentsCount = calcStatusCount(status?.comments_count ?? 0, defaultTitle: "评论")
            attitudesCount = calcStatusCount(status?.attitudes_count ?? 0, defaultTitle: "赞")
            
            if let retweetText = status?.retweeted_status?.text {
                if let name = status?.retweeted_status?.user?.screen_name {
                    retweetStatusText = "@\(name):\(retweetText)"
                }
            }

        }
    }
    
    // 会员显示的图片
    var memberImage: UIImage?
    // 认证图片
    var verifiedImage: UIImage?
    
    // 转发评论赞的数量字符串
    var repostsCount: String?
    var commentsCount: String?
    var attitudesCount: String?
    /// 转发微博的内容(如果有转发微博的话，这个属性才有意义)
    var retweetStatusText: String? // @昵称: 转发微博的内容
    
    //MARK:请算转发市评论赞的数量字符串
    private func calcStatusCount(count:Int,defaultTitle:String)->(String){
        
        if count == 0{
         return defaultTitle
        }
        
        if count < 10000{
            return "\(count)"
        }else{
            let result = CGFloat(count/1000)/10
            let resultString = "\(result)万"
            if resultString.containsString(".0"){
                return resultString.stringByReplacingOccurrencesOfString(".0", withString: "")
            }
            return resultString
        }
    }
    
    /// 计算认证的图标
    private func calcVerifiedImage() -> UIImage? {
        // 计算认证图标
        if let verifiedType = status?.user?.verified_type {
            switch verifiedType {
            case -1:
                return nil
            case 0:
                return UIImage(named: "avatar_vip")
            case 2,3,5:
                return UIImage(named: "avatar_enterprise_vip")
            case 220:
                return UIImage(named: "avatar_grassroot")
            default:
                return nil
            }
        }
        return nil
    }
    
    override var description: String {
        let keys = ["status"]
        return dictionaryWithValuesForKeys(keys).description
    }

}
